Algorithmic Thinking, 2nd Edition (for Tinh Hong) by Daniel Zingaro
Author:Daniel Zingaro
Language: eng
Format: epub
Publisher: No Starch Press, Inc.
Published: 2024-11-15T00:00:00+00:00
Two Optimizations
There are a few things that can be done to speed up Dijkstraâs algorithm. The most widely applicable and dramatic speedup is wrought by a data structure called a heap. In our current implementation, itâs very expensive to find the next node to set to done, as we need to scan through all nodes that are not done to find the one with the shortest path. A heap uses a tree to convert this slow, linear search into a fast search. As heaps are useful in many contexts beyond Dijkstraâs algorithm, Iâll discuss them later, in Chapter 8. Here, Iâll offer a couple of optimizations more specific to the Mice Maze problem.
Recall that as soon as a cell is done, we never change its shortest path again. As such, once we set the exit cell to done, we have its shortest path. After that, thereâs no reason to find shortest paths for other cells. We may as well terminate Dijkstraâs algorithm early.
We can still do better, though. For a maze of n cells, we invoke Dijkstraâs algorithm n times, once for each cell. For Cell 1, we compute all shortest pathsâand then keep only the shortest path to the exit cell. We do the same for Cell 2, Cell 3, and so on, throwing out all of the shortest paths we found except for those that involve the exit cell.
Instead, consider running Dijkstraâs algorithm just once, with the exit cell as the starting cell. Dijkstraâs algorithm would then find the shortest path from the exit cell to Cell 1, the exit cell to Cell 2, and so on. However, thatâs not quite what we want, because the graph is directed: the shortest path from the exit cell to Cell 1 is not necessarily the shortest path from Cell 1 to the exit cell.
Here again is Figure 6-1:
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
C | C++ |
Tutorials | Visual C++ |
Hello! Python by Anthony Briggs(9867)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9757)
The Mikado Method by Ola Ellnestam Daniel Brolund(9747)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7745)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7741)
Grails in Action by Glen Smith Peter Ledbrook(7667)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7517)
Windows APT Warfare by Sheng-Hao Ma(6502)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6378)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6248)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6119)
Kotlin in Action by Dmitry Jemerov(5019)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4297)
Functional Programming in JavaScript by Mantyla Dan(4018)
Solidity Programming Essentials by Ritesh Modi(3839)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3614)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3566)
The Ultimate iOS Interview Playbook by Avi Tsadok(3532)
